package com.ccrfid.rmc6.resource.sys;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.sys.OrgEntity;
import com.ccrfid.rmc6.service.sys.OrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("orgs")
@Produces(MediaType.APPLICATION_JSON)
public class OrgResource {
    private static final Logger logger = LoggerFactory.getLogger(OrgResource.class);
    @Autowired
    OrgService orgService;

    @GET
    @Path("type/{typeId}")
    public List<OrgEntity> getOrgListByType(@PathParam("typeId") Integer typeId) {
        List<OrgEntity> list = orgService.getOrgListByType(typeId);
        return list;
    }

    @GET
    public List<OrgEntity> getOrgList() {
        List<OrgEntity> list = orgService.getOrgList();
        return list;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public OrgEntity createOrg(OrgEntity entity) throws AppException {
        OrgEntity orgEntity = orgService.createOrg(entity);
        return orgEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteOrgs(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return orgService.deleteOrgs(idList);
    }

    @GET
    @Path("{id}")
    public OrgEntity getOrgInfoById(@PathParam("id") Long orgId) {
        OrgEntity orgEntity = orgService.getOrgInfoById(orgId);
        return orgEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public OrgEntity updateOrg(@PathParam("id") Long orgId, OrgEntity entity) throws AppException {
        OrgEntity orgEntity = orgService.updateOrg(orgId, entity);
        return orgEntity;
    }

    @GET
    @Path("{id}/subs")
    public List<OrgEntity> getSubOrgList(@PathParam("id") Long orgId) {
        List<OrgEntity> list = orgService.getSubOrgList(orgId);
        return list;
    }

    @GET
    @Path("{id}/id-subs")
    public List<Long> getSubOrgListL(@PathParam("id") Long orgId) {
        List<Long> list = orgService.getSubOrgIdList(orgId);
        return list;
    }

    @GET
    @Path("sn/{sn}")
    public OrgEntity getOrgInfoBySn(@PathParam("sn") String orgSn) {
        OrgEntity orgEntity = orgService.getOrgInfoBySn(orgSn);
        return orgEntity;
    }
}
